package com.aotain.coeus.unregistered.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.aotain.common.CommonDB;
import com.aotain.common.CommonUtil;
import com.aotain.common.DBConfigInit;
import com.aotain.common.DBObject;

public class RecordIntfDao {

	private static RecordIntfDao instance  ;
	
	DBConfigInit dbConf = null;
	
	DBObject dbo = null;
	
	Connection conn = null;
	
	ResultSet rs = null;
	
	PreparedStatement pstmt = null;
	
	public String _dbconfig = CommonUtil.DBConfigFile;
	
	private RecordIntfDao(){
//		dbConf = new DBConfigInit("E:/Myworkspace/hades-project/config/dbconfig.ini");
		dbConf = new DBConfigInit("/home/storm/config/dbconfig_oracle.ini");
		_dbconfig = "/home/storm/config/dbconfig_oracle.ini";
		dbo = dbConf.getDBObj();
	}
	
	
	public static RecordIntfDao getInstance() {
		if(instance == null) {
			synchronized (RecordIntfDao.class) {
				return new RecordIntfDao();
			}
		}
		return instance;
	}
	
	public Map<String,String> getAllStatus() {
//		List<RecordIntfBean> rsList = new ArrayList<RecordIntfBean>();
		Map<String,String> map = new HashMap<String,String>();
		conn = CommonDB.getConnection(dbo.getDriverClassName(), dbo.getUrl(), dbo.getUserName(), dbo.getPassword());
		String sql = "select t.domain ,t.status from SDS_RC_RECORDINTF t";
		
		long start = System.currentTimeMillis();
		System.out.println("&&&&&&&&&&&&&&startTime : " + System.currentTimeMillis());
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();	
			while(rs.next()) {
//				RecordIntfBean record = new RecordIntfBean();
//				record.setDomain(rs.getString(1));
//				record.setStatus(rs.getString(2));
//				rsList.add(record);
				map.put(rs.getString(1), rs.getString(2));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			CommonDB.close(rs, pstmt, conn);
		}
		long end = System.currentTimeMillis();
		System.out.println("&&&&&&&&&&&&&&&&&&&&&&GetAllStatus() use Time :" + (end - start));
		return map;
	}
	
	public Map<String,String> getAllRecordNum() {
//		List<RecordIntfBean> rsList = new ArrayList<RecordIntfBean>();
		Map<String,String> map = new HashMap<String,String>();
		conn = CommonDB.getConnection(dbo.getDriverClassName(), dbo.getUrl(), dbo.getUserName(), dbo.getPassword());
		String sql = "select t.domain ,t.recordnum from SDS_RC_RECORDINTF t";
		
		long start = System.currentTimeMillis();
		System.out.println("&&&&&&&&&&&&&&startTime : " + System.currentTimeMillis());
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();	
			while(rs.next()) {
//				RecordIntfBean record = new RecordIntfBean();
//				record.setDomain(rs.getString(1));
//				record.setStatus(rs.getString(2));
//				rsList.add(record);
				map.put(rs.getString(1), rs.getString(2));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			CommonDB.close(rs, pstmt, conn);
		}
		long end = System.currentTimeMillis();
		System.out.println("&&&&&&&&&&&&&&&&&&&&&&GetAllRecordNum() use Time :" + (end - start));
		return map;
	}
	
	public static void main(String[] args) {
//		System.out.println(RecordInifDao.class.getResource("."));
		long start = System.currentTimeMillis();
		Map<String,String> map =  RecordIntfDao.getInstance().getAllRecordNum();
		long end = System.currentTimeMillis();
		int n = 0;
		for(Entry<String,String> entry : map.entrySet()) {
			if(n<10)
			System.out.println( entry.getKey() + " :" + entry.getValue() + ";");
			n++;
		}
//		System.out.println(list.size() + " : " + (end - start));
	}
}
